package com.jaya.budan.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jaya.budan.R;
import com.jaya.budan.bean.ListUser_Information;

import java.util.List;

public class TaRen_HorizontalListViewAdapter extends BaseAdapter {

    private Context mcontext;
    private int screenWidth, is_allow;
    private List<ListUser_Information> mlist;
    public TaRen_HorizontalListViewAdapter(Context context, int width, List<ListUser_Information> list, int allow){
        mcontext = context;
        screenWidth = width;
        is_allow = allow;
        mlist = list;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = View.inflate(mcontext, R.layout.taren_main_listview_item, null);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.ll_main_item = convertView.findViewById(R.id.ll_main_item);
        holder.iv_taren_main_item = convertView.findViewById(R.id.iv_taren_main_item);
        holder.iv_main_item = convertView.findViewById(R.id.iv_main_item);
        //计算每条的宽度
        ViewGroup.LayoutParams layoutParams = holder.ll_main_item.getLayoutParams();
        layoutParams.width = screenWidth / 7 * 2;
        holder.ll_main_item.setLayoutParams(layoutParams);
        if (is_allow == 0){ //是否允许查看 1允许 0不允许
            holder.iv_taren_main_item.setVisibility(View.VISIBLE);
            Glide.with(mcontext).load(mlist.get(position).getThumb_path()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(holder.iv_main_item);
        } else {
            holder.iv_taren_main_item.setVisibility(View.GONE);
            Glide.with(mcontext).load(mlist.get(position).getReal_path()).apply(new RequestOptions()
                    .placeholder(R.mipmap.iv_main_item).error(R.mipmap.iv_main_item)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//关闭Glide的硬盘缓存机制
            ).into(holder.iv_main_item);
        }
        return convertView;
    }

    public class ViewHolder {
        RelativeLayout ll_main_item;
        ImageView iv_taren_main_item, iv_main_item;
    }
}
